<template>
    <div v-loading="loading">
        <CommonPage :state="state" :flowId="infoData.flowId" @submitForm="submitForm">
            <Info style="padding: 20px" ref="form" v-model="infoData" :state="state"></Info>
        </CommonPage>
        <!--        <div v-if="'write'==state" class="card-wrap flex justify-end card-padding">-->
        <!--            <el-button :loading="loading" type="primary" @click="submitForm(0)">保存</el-button>-->
        <!--            <el-button @click="cancel">取消</el-button>-->
        <!--            <el-button :loading="loading" type="primary" @click="submitForm(1)">提交</el-button>-->
        <!--        </div>-->

    </div>
</template>

<script>


import {
    driverCompanyDetail,
    driverCompanyUpdate,
    driverCompanyAdd,
    driverCompanyFlowAdd
} from "@/api/memberManage/driverList";
import {supplierAdd, supplierUpdate} from "@/api/memberManage/vendorList";
import Info from "@/views/driverManage/laberCompany/components/info.vue";

export default {
    name: "laberCompanyAdd",
    components: {Info},
    data() {
        return {
            loading: false,
            infoData: {},
            state: 'write'
        };
    },
    watch:{
        '$route':{
            handler(val){
                console.log('this.$route', this.$route)

                if(val && val.name==this.$options.name){
                    if (this.$route.query && this.$route.query.companyId && !this.$route.query.tabClick) {
                        this.getOldData(this.$route.query.companyId)
                    }
                    else if(!this.$route.query.tabClick){
                        this.infoData = {}
                    }
                    this.state = this.$route?.query?.type

                    const title = this.$route?.query?.pageType ?? '劳务公司'
                    const route = Object.assign({}, this.$route, {title: `${title}`})
                    this.$store.dispatch('tagsView/updateVisitedView', route)
                }
            },
            immediate:true,
            deep:true
        }
    },
    methods: {
        cancel() {
            this.$router.back();
            this.$store.dispatch('tagsView/delView', this.$route);
        },
        getOldData(companyId) {
            this.loading = true;
            driverCompanyDetail(companyId).then(res => {
                this.infoData = res.data
            }).finally(() => {
                this.loading = false;
            });
        },
        async submitForm(type) {
            let that = this


            this.infoData.approvalStatus = type

            if (0 == type) {
                that.commit()
            } else if (1 == type) {
                let result = await this.$refs.form.valid();
                // this.$refs["form"].validate(valid => {
                if (result) {
                    that.flowCommit()
                    // that.commit()
                }
                // });
            }

        },
        flowCommit() {
            let that = this
            this.loading = true;
            driverCompanyFlowAdd(that.infoData).then(res => {
                this.loading = false;
                this.$modal.msgSuccess("提交成功");
                this.$router.back();
                this.$store.dispatch('tagsView/delView', this.$route);
                ;
                this.$store.dispatch('tagsView/delView', this.$route);
            }).finally(e => {
                this.loading = false;
            })
        },
        commit() {
            let that = this
            this.loading = true;

            if (that.infoData.companyId) {
                driverCompanyUpdate(that.infoData).then(res => {
                    this.loading = false;
                    this.$modal.msgSuccess("保存成功");
                    this.$router.back();
                    this.$store.dispatch('tagsView/delView', this.$route);
                }).finally(e => {
                    this.loading = false;
                })
            } else {
                driverCompanyAdd(that.infoData).then(res => {
                    this.loading = false;
                    this.$modal.msgSuccess("保存成功");
                    this.$router.back();
                    this.$store.dispatch('tagsView/delView', this.$route);
                }).finally(e => {
                    this.loading = false;
                })
            }
        },

    }
};
</script>


<style lang="scss" scoped>
::v-deep .el-form--label-top .el-form-item__label {
    padding: 0;
}

.el-form-item {
    margin-bottom: 15px;
    width: 70%;
}

::v-deep .el-date-editor--daterange {
    width: 100% !important;
}

::v-deep .el-select {
    width: 100%;
}

::v-deep .el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: 100%;
}


</style>
